<?php
/**
 * This Test creates an order with 3 OrdersProducts, 
 * payed in Euros instead of the standard Dollars
 */
require_once(TEST_INTEGRATION_PATH . 'OrderFactory.php');
require_once(TEST_DTO_PATH.'ProductDTO.php');
require_once(TEST_DTO_PATH.'ProductOptionDTO.php');
require_once(TEST_DTO_PATH.'OrderDTO.php');

class TestOfOrderWithMultipleProductsAttributes extends UnitTestCase {
	private $db;
	private $order;
	private $productDTO;
	private $productOptionDTO;
	private $orderDTO;
	
	function __construct() {
		$this->db = new Datasource(TEST_DB_SERVER, TEST_DB_DATABASE, TEST_DB_SERVER_USERNAME, TEST_DB_SERVER_PASSWORD);
		
		$this->createSchema();
		
		$p1option1 = new ProductOptionDTO;
		$p1option1->options_id = 1;
		$p1option1->options_values_id = 15;
		//$this->option1->options_values_name = "Blue";		
		
		$p1option2 = new ProductOptionDTO;
		$p1option2->options_id = 4;
		$p1option2->options_values_id = 2;
		//$this->option2->options_values_name = "8 mb";
		
		$p1= new ProductDTO;
		$p1->pid = 59;
		$p1->options = array($p1option1, $p1option2);
		
		
		$p2option1 = new ProductOptionDTO;
		$p2option1->options_id = 1;
		$p2option1->options_values_id = 25;
		//$this->option1->options_values_name = "Orange";
		
		$p2option2 = new ProductOptionDTO;
		$p2option2->options_id = 2;
		$p2option2->options_values_id = 18;
		//$this->option1->options_values_name = "Medium";
		
		$p2option3 = new ProductOptionDTO;
		$p2option3->options_id = 13;
		$p2option3->options_values_id = 36;
		//$this->option1->options_values_name = "Collector's Tin";
		
		$p2 = new ProductDTO;
		$p2->pid = 78;
		$p2->options = array($p2option1, $p2option2, $p2option3);
		
		$cashierDTO = new CashierDTO;
		$cashierDTO->name = "Rudi";
		
		$this->orderDTO = new OrderDTO();
		$this->orderDTO->products = array($p1,$p2);
		$this->orderDTO->price = 500.39;
		$this->orderDTO->currency = "GBP";
		$this->orderDTO->method = "cod";
		$this->orderDTO->cashier = $cashierDTO;
		
		$this->order = new OrderFactory();
	}
	
	private function createSchema() {
		/* This order has been made via the Zen Cart Front-End to have a direct comparisson of values */
		$this->db->execute("INSERT INTO `orders` (`orders_id` ,`customers_id` ,`customers_name` ,`customers_company` ,`customers_street_address` ,`customers_suburb` ,`customers_city` ,`customers_postcode` ,`customers_state` ,`customers_country` ,`customers_telephone` ,`customers_email_address` ,`customers_address_format_id` ,`delivery_name` ,`delivery_company` ,`delivery_street_address` ,`delivery_suburb` ,`delivery_city` ,`delivery_postcode` ,`delivery_state` ,`delivery_country` ,`delivery_address_format_id` ,`billing_name` ,`billing_company` ,`billing_street_address` ,`billing_suburb` ,`billing_city` ,`billing_postcode` ,`billing_state` ,`billing_country` ,`billing_address_format_id` ,`payment_method` ,`payment_module_code` ,`shipping_method` ,`shipping_module_code` ,`coupon_code` ,`cc_type` ,`cc_owner` ,`cc_number` ,`cc_expires` ,`last_modified` ,`date_purchased` ,`orders_status` ,`orders_date_finished` ,`currency` ,`currency_value` ,`order_total` ,`order_tax` ,`paypal_ipn_id` ,`ip_address`) VALUES ('6', '1', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '12345', 'root@localhost', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Cash on Delivery', 'cod', 'Store Pickup (Walk In)', 'storepickup', '', '', '', '', '', NULL , '2008-02-24 19:37:37', '3', NULL , 'GBP', '0.503000', '500.39', '0.00', '0', '127.0.0.1 - 127.0.0.1')");
		$this->db->execute("INSERT INTO `orders_products` (`orders_products_id` ,`orders_id` ,`products_id` ,`products_model` ,`products_name` ,`products_price` ,`final_price` ,`products_tax` ,`products_quantity` ,`onetime_charges` ,`products_priced_by_attribute` ,`product_is_free` ,`products_discount_type` ,`products_discount_type_from` ,`products_prid`) VALUES ('14', '6', '59', 'HPLJ1100XI', 'Hewlett Packard - by attributes', '0.0000', '369.1383', '0.0000', '1', '0.0000', '1', '0', '0', '0', '59:fb869385272ae7f6f5202b39913b2f1a') ");
		$this->db->execute("INSERT INTO `orders_products` (`orders_products_id` ,`orders_id` ,`products_id` ,`products_model` ,`products_name` ,`products_price` ,`final_price` ,`products_tax` ,`products_quantity` ,`onetime_charges` ,`products_priced_by_attribute` ,`product_is_free` ,`products_discount_type` ,`products_discount_type_from` ,`products_prid`) VALUES ('15', '6', '78', 'Test25-10AttrAll', 'TEST 25% special 10% Sale Attribute Priced', '0.0000', '131.2500', '0.0000', '1', '0.0000', '1', '0', '0', '0', '78:2de6536fc24a1969dd4fc0d00a69c353')");
		$this->db->execute("INSERT INTO `orders_products_attributes` (`orders_products_attributes_id` ,`orders_id` ,`orders_products_id` ,`products_options` ,`products_options_values` ,`options_values_price` ,`price_prefix` ,`product_attribute_is_free` ,`products_attributes_weight` ,`products_attributes_weight_prefix` ,`attributes_discounted` ,`attributes_price_base_included` ,`attributes_price_onetime` ,`attributes_price_factor` ,`attributes_price_factor_offset` ,`attributes_price_factor_onetime` ,`attributes_price_factor_onetime_offset` ,`attributes_qty_prices` ,`attributes_qty_prices_onetime` ,`attributes_price_words` ,`attributes_price_words_free` ,`attributes_price_letters` ,`attributes_price_letters_free` ,`products_options_id` ,`products_options_values_id` ,`products_prid`) VALUES ('6', '6', '11', 'Color', 'Blue', '539.0000', '', '0', '0', '+', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '1', '15', '59:fb869385272ae7f6f5202b39913b2f1a')");
		$this->db->execute("INSERT INTO `orders_products_attributes` (`orders_products_attributes_id` ,`orders_id` ,`orders_products_id` ,`products_options` ,`products_options_values` ,`options_values_price` ,`price_prefix` ,`product_attribute_is_free` ,`products_attributes_weight` ,`products_attributes_weight_prefix` ,`attributes_discounted` ,`attributes_price_base_included` ,`attributes_price_onetime` ,`attributes_price_factor` ,`attributes_price_factor_offset` ,`attributes_price_factor_onetime` ,`attributes_price_factor_onetime_offset` ,`attributes_qty_prices` ,`attributes_qty_prices_onetime` ,`attributes_price_words` ,`attributes_price_words_free` ,`attributes_price_letters` ,`attributes_price_letters_free` ,`products_options_id` ,`products_options_values_id` ,`products_prid`) VALUES ('7', '6', '11', 'Memory', '8 mb', '75.0000', '+', '0', '1', '+', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '4', '2', '59:fb869385272ae7f6f5202b39913b2f1a')");
		$this->db->execute("INSERT INTO `orders_products_attributes` (`orders_products_attributes_id` ,`orders_id` ,`orders_products_id` ,`products_options` ,`products_options_values` ,`options_values_price` ,`price_prefix` ,`product_attribute_is_free` ,`products_attributes_weight` ,`products_attributes_weight_prefix` ,`attributes_discounted` ,`attributes_price_base_included` ,`attributes_price_onetime` ,`attributes_price_factor` ,`attributes_price_factor_offset` ,`attributes_price_factor_onetime` ,`attributes_price_factor_onetime_offset` ,`attributes_qty_prices` ,`attributes_qty_prices_onetime` ,`attributes_price_words` ,`attributes_price_words_free` ,`attributes_price_letters` ,`attributes_price_letters_free` ,`products_options_id` ,`products_options_values_id` ,`products_prid`) VALUES ('8', '6', '12', 'Color', 'Orange', '50.0000', '+', '0', '0', '+', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '1', '25', '78:2de6536fc24a1969dd4fc0d00a69c353')");		
		$this->db->execute("INSERT INTO `orders_products_attributes` (`orders_products_attributes_id` ,`orders_id` ,`orders_products_id` ,`products_options` ,`products_options_values` ,`options_values_price` ,`price_prefix` ,`product_attribute_is_free` ,`products_attributes_weight` ,`products_attributes_weight_prefix` ,`attributes_discounted` ,`attributes_price_base_included` ,`attributes_price_onetime` ,`attributes_price_factor` ,`attributes_price_factor_offset` ,`attributes_price_factor_onetime` ,`attributes_price_factor_onetime_offset` ,`attributes_qty_prices` ,`attributes_qty_prices_onetime` ,`attributes_price_words` ,`attributes_price_words_free` ,`attributes_price_letters` ,`attributes_price_letters_free` ,`products_options_id` ,`products_options_values_id` ,`products_prid`) VALUES ('9', '6', '12', 'Size', 'Medium', '75.0000', '+', '0', '0', '+', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '2', '18', '78:2de6536fc24a1969dd4fc0d00a69c353')");
		$this->db->execute("INSERT INTO `orders_products_attributes` (`orders_products_attributes_id` ,`orders_id` ,`orders_products_id` ,`products_options` ,`products_options_values` ,`options_values_price` ,`price_prefix` ,`product_attribute_is_free` ,`products_attributes_weight` ,`products_attributes_weight_prefix` ,`attributes_discounted` ,`attributes_price_base_included` ,`attributes_price_onetime` ,`attributes_price_factor` ,`attributes_price_factor_offset` ,`attributes_price_factor_onetime` ,`attributes_price_factor_onetime_offset` ,`attributes_qty_prices` ,`attributes_qty_prices_onetime` ,`attributes_price_words` ,`attributes_price_words_free` ,`attributes_price_letters` ,`attributes_price_letters_free` ,`products_options_id` ,`products_options_values_id` ,`products_prid`) VALUES ('10', '6', '12', 'Gift Options', 'Collector''s Tin', '50.0000', '+', '0', '0', '+', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '13', '36', '78:2de6536fc24a1969dd4fc0d00a69c353')");
		$this->db->execute(" INSERT INTO `orders_status_history` (`orders_status_history_id` ,`orders_id` ,`orders_status_id` ,`date_added` ,`customer_notified` ,`comments`) VALUES ('7', '5', '1', '2008-02-24 19:37:37', '1', '')");
		$this->db->execute(" INSERT INTO `orders_total` (`orders_total_id` ,`orders_id` ,`title` ,`text` ,`value` ,`class` ,`sort_order`) VALUES ('15', '5', 'Total:', '&pound;251.70', '500.3900', 'ot_total', '999')");
	}
	
	private function dropSchema() {
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 6");
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 7");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 6");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 7");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 6");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 7");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 6");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 7");
	}
	
	function testOrderExecute() {
		$this->assertTrue($this->order->execute($this->orderDTO));
	}

	function testOrdersTable() {
		// comparing all fields except the orders_id, date_purchased, and ip_address
		$originalorder =	$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =6");
		$neworder =			$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =7");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
/*
	function testOrdersProductsTable() {
		// comparing all fields except orders_id and products_prid
		// this should have three rows corresponding to the 3 ordered products
		$originalorder =	$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` , `products_prid` FROM `orders_products` WHERE `orders_id` =6");
		$neworder =			$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` , `products_prid` FROM `orders_products` WHERE `orders_id` =7");
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
		$this->assertEqual($this->db->nextRow($originalorder), $this->db->nextRow($neworder));
	}

	function testOrdersTotalTable() {
		// comparing only ot_total without the fields orders_total_id and orders_id
		$originalorder =	$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =6 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$neworder = 		$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =7 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
*/
	function testOrdersProductsAttributesTable() {
		$originalorder =	$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =6");
		$neworder = 		$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =7");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsQuantityReduction() {
		// 59 has 700 in the inventory to start
		$newquantityProduct59 = $this->db->execute("SELECT `products_quantity` FROM `products` WHERE products_id =59");
		$this->assertEqual(699, (int)mysql_result($newquantityProduct59, "products_quantity"));
		
		// 78 has 1000 in the inventory to start
		$newquantityProduct78 = $this->db->execute("SELECT `products_quantity` FROM `products` WHERE products_id =78");
		$this->assertEqual(999, (int)mysql_result($newquantityProduct78, "products_quantity"));
		//$this->dropSchema();
	}
	
}

?>
